import App from "./App";
// 引入全局存储
import store from "@/store";
// 引入全局路由
import routes, { noIntercept } from "@/config/routes.config.js";
import apiConfig from "@/config/api.config.js";
import { Token } from "@/utils/constant";
import { getStorageItem } from "@/utils/storage.js";
import { showModal } from "@/utils/util.js";

// 网络状态监听
uni.getNetworkType({
  success: (res) => {
    console.log(res);
    store.dispatch("networkStateChange", res.networkType);
  },
});
uni.onNetworkStatusChange((res) => {
  console.log(res);
  store.dispatch("networkStateChange", res.networkType);
});

// #ifndef VUE3
import Vue from "vue";
Vue.prototype.$rConfig = routes;
Vue.prototype.$apiConfig = apiConfig;
Vue.config.productionTip = false;
App.mpType = "app";

import uView from "uview-ui";
Vue.use(uView);

const app = new Vue({
  store,
  ...App,
});

// http拦截器，此为需要加入的内容，如果不是写在common目录，请自行修改引入路径
import httpInterceptor from "@/common/http.interceptor.js";
// 这里需要写在最后，是为了等Vue创建对象完成，引入"app"对象(也即页面的"this"实例)
Vue.use(httpInterceptor, app);

// http接口API集中管理引入部分
import httpApi from "@/common/http.api.js";
Vue.use(httpApi, app);

app.$mount();

// 路由跳转拦截
Vue.prototype.$u.routeIntercept = (params, resolve) => {
  if (noIntercept.includes(params.url)) {
    // 无需校验登陆状态
    resolve(true);
  } else {
    // 需要校验
    const token = getStorageItem(Token) || "";
    if (token) {
      resolve(true);
    } else {
      resolve(false);
      showModal({
        content: "当前账号未登录，请登陆后进行操作",
      }).then((res) => {
        if (!res) {
        } else {
          uni.$u.route(routes.login);
        }
      });
    }
  }
};
// #endif

// #ifdef VUE3
import { createSSRApp } from "vue";
export function createApp() {
  const app = createSSRApp(App);
  return {
    app,
  };
}
// #endif
